/**
 * Poker game. Hold'em Limit Version
 *
 *All cards are listed according to the links below.
 *for example: hK - king of hearts, sT - 10 of spades, dJ - Jack of diamonds, c4 - four of clubs
 *d - diamonds
 *s - spades
 *h - hearts
 *c - clubs
 */
//links to the cards images 
var cardlinks =["images/cards/d/d2.jpg", "images/cards/d/d3.jpg", "images/cards/d/d4.jpg",
                "images/cards/d/d5.jpg", "images/cards/d/d6.jpg", "images/cards/d/d7.jpg",
                "images/cards/d/d8.jpg", "images/cards/d/d9.jpg", "images/cards/d/dT.jpg",
                "images/cards/d/dJ.jpg", "images/cards/d/dQ.jpg", "images/cards/d/dK.jpg",
                "images/cards/d/dA.jpg", "images/cards/s/s2.jpg", "images/cards/s/s3.jpg",
                "images/cards/s/s4.jpg", "images/cards/s/s5.jpg", "images/cards/s/s6.jpg",
                "images/cards/s/s7.jpg", "images/cards/s/s8.jpg", "images/cards/s/s9.jpg",
                "images/cards/s/sT.jpg", "images/cards/s/sJ.jpg", "images/cards/s/sQ.jpg",
                "images/cards/s/sK.jpg", "images/cards/s/sA.jpg", "images/cards/h/h2.jpg",
                "images/cards/h/h3.jpg", "images/cards/h/h4.jpg", "images/cards/h/h5.jpg",
                "images/cards/h/h6.jpg", "images/cards/h/h7.jpg", "images/cards/h/h8.jpg",
                "images/cards/h/h9.jpg", "images/cards/h/hT.jpg", "images/cards/h/hJ.jpg",
                "images/cards/h/hQ.jpg", "images/cards/h/hK.jpg", "images/cards/h/hA.jpg",
                "images/cards/c/c2.jpg", "images/cards/c/c3.jpg", "images/cards/c/c4.jpg",
                "images/cards/c/c5.jpg", "images/cards/c/c6.jpg", "images/cards/c/c7.jpg",
                "images/cards/c/c8.jpg", "images/cards/c/c9.jpg", "images/cards/c/cT.jpg",
                "images/cards/c/cJ.jpg", "images/cards/c/cQ.jpg", "images/cards/c/cK.jpg",
                "images/cards/c/cA.jpg"];
//player banks(money the've got at the start of the game)	
var banks = [100, 100, 100, 100, 100, 100, 100, 100, 100];
//bets that players make
var plbet = [0, 0, 0, 0, 0, 0, 0, 0, 0];
//True if the player didn't fold the cards
var ingame = [1, 1, 1, 1, 1, 1, 1, 1, 1];
//Current dealer in the game
var dealer=0;
//Phase of the game(0 - Preflop, 1 - flop, 2 - turn, 3- river)
var move=0;
var time = 1000;
var gamecounter=0;

var ingamen=9;
var counter=0;
var sb = 2;//small blind
var bb = 4;//big blind
var bank = 0;//bank
var bet = 4;//smallest bet
// Current player
var k = 0;


//When the document loads, poker starts immediately:
$(document).ready(function() {
	//Is activated when document loads
	hidebuttons();
	animatetable();
	activatechat();
	popdata();
});
//THE GAME START
function startgame(){
	/*jQuery.fx.off = true; //Turn off animations*/
	//Randomly Choose the first dealer
	pushdata();
	$.post("Engine?newcards");
	$.get("Engine?plcards=1",function(data){
		numbers=data.split(" ");
		$("#player1_1").attr("src", cardlinks[numbers[0]]);
		$("#player1_2").attr("src", cardlinks[numbers[1]]);
	},"text");
	dealcards(1,1);
	$.get("Engine?nextdealer",function(data){
		dealers(parseInt(data));
		//dealer=parseInt(data);
	},"text");
	$("#bank").html(bank);
	gamecounter+=1;
	setTimeout('plmove()',5000);
};
function pushdata(){
	$.post("Engine?move="+move);
	$.post("Engine?dealer="+dealer);
	message="Engine?";
	for (i=0;i<ingame.length;i++){
		if (i==0) message+="ingame="+ingame[i];
		else message+="&ingame="+ingame[i];
	}
	$.post(message);
	message="Engine?";
	for (i=0;i<banks.length;i++){
		if (i==0) message+="banks="+banks[i];
		else message+="&banks="+banks[i];
	}
	$.post(message);
}
function popdata(){
	$.get("Engine?gamestart",function(data){
		data=data.split(" ");
		move=parseInt(data[0]);
		dealer=parseInt(data[1]);
		//$("#pl1_bank").html(data[20]);
		for (i=2;i<ingame.length+2;i++){
			ingame[i-2]=parseInt(data[i]);
			banks[i-2]=parseInt(data[i+ingame.length]);
			$("#pl"+(i-2)+"_bank").html(data[i+ingame.length]);
		}
		if (move==0){
			addyourname();//change name
			$.post("Engine?plname="+$("#pl1_name").html());
			setTimeout('startgame()',500);
		}
		else{
			$("img#dealer"+dealer).css("opacity", "1");
			for (var i=1;i<=ingame.length;i++){
				if (ingame[i-1]==1){
					$("#player"+i+"_1").css("opacity", "1");
					$("#player"+i+"_2").css("opacity", "1");
					if (i==1){
						$.get("Engine?plcards=1",function(data){
							numbers=data.split(" ");
							$("#player1_1").attr("src", cardlinks[numbers[0]]);
							$("#player1_2").attr("src", cardlinks[numbers[1]]);
						},"text");
					}
				}
			}
			
			if (move==1){
				setTimeout('flop()',500);
			}
			else if (move==2){
				$.get("Engine?flopcards=1",function(data){
					$("img#t_card1").attr("src", cardlinks[parseInt(data)]);
					$("img#t_card1").css("opacity", "1");
				},"text");
				$.get("Engine?flopcards=2",function(data){
					$("img#t_card2").attr("src", cardlinks[parseInt(data)]);
					$("img#t_card2").css("opacity", "1");
				},"text");
				$.get("Engine?flopcards=3",function(data){
					$("img#t_card3").attr("src", cardlinks[parseInt(data)]);
					$("img#t_card3").css("opacity", "1");
				},"text");
				setTimeout('turn()',500);
			}
			else if (move==3){
				$.get("Engine?flopcards=1",function(data){
					$("img#t_card1").attr("src", cardlinks[parseInt(data)]);
					$("img#t_card1").css("opacity", "1");
				},"text");
				$.get("Engine?flopcards=2",function(data){
					$("img#t_card2").attr("src", cardlinks[parseInt(data)]);
					$("img#t_card2").css("opacity", "1");
				},"text");
				$.get("Engine?flopcards=3",function(data){
					$("img#t_card3").attr("src", cardlinks[parseInt(data)]);
					$("img#t_card3").css("opacity", "1");
				},"text");
				$.get("Engine?flopcards=4",function(data){
					$("img#t_card4").attr("src", cardlinks[parseInt(data)]);
					$("img#t_card4").css("opacity", "1");
				},"text");
				setTimeout('river()',500);
			}
			else{
				restart();
			}
		}
	},"text");
}
function restartgame(){
	//Refresh the window, that makes the game to restart
	var x=confirm("Are you sure you want to restart the game?");
	if (x==true){
		$.post("Engine?restart",function(data){
			history.go();
		});
	}
}
//MOVES
function flop(){
	counter=0;
	ingamen=ingame.sum();
	move = 1;
	pushdata();
	bank += plbet.sum();
	$("#bank").html(bank);
	$("div.playerbet").css("opacity", "0");
	plbet = [0, 0, 0, 0, 0, 0, 0, 0, 0];
	bet = 0;
	$.get("Engine?flopcards=1",function(data){
		$("img#t_card1").attr("src", cardlinks[parseInt(data)]);
	},"text");
	$.get("Engine?flopcards=2",function(data){
		$("img#t_card2").attr("src", cardlinks[parseInt(data)]);
	},"text");
	$.get("Engine?flopcards=3",function(data){
		$("img#t_card3").attr("src", cardlinks[parseInt(data)]);
	},"text");
	dealtcards(1,3);
	k=dealer+1;
	setTimeout('plmove()',3000);
}
function turn(){
	counter=0;
	ingamen=ingame.sum();
	move = 2;
	pushdata();
	bank += plbet.sum();
	$("#bank").html(bank);
	$("div.playerbet").css("opacity", "0");
	plbet = [0, 0, 0, 0, 0, 0, 0, 0, 0];
	bet = 0;
	bb = bb*2;
	$.get("Engine?flopcards=4",function(data){
		$("img#t_card4").attr("src", cardlinks[parseInt(data)]);
	},"text");
	dealtcards(4,4);
	k=dealer+1;
	setTimeout('plmove()',3000);
}
function river(){
	counter=0;
	ingamen=ingame.sum();
	move = 3;
	pushdata();
	bank += plbet.sum();
	$("#bank").html(bank);
	$("div.playerbet").css("opacity", "0");
	plbet = [0, 0, 0, 0, 0, 0, 0, 0, 0];
	bet = 0;
	$.get("Engine?flopcards=5",function(data){
		$("img#t_card5").attr("src", cardlinks[parseInt(data)]);
	},"text");
	k=dealer+1;
	dealtcards(5,5);
	setTimeout('plmove()',3000);
}
//PLAY
function plmove(){
	//The AI of the computer
	activatechat();
	if (k >= 10){
		k = k-9;
	}
	time=1000;
	if(ingame[k-1]==1){
		counter+=1;
		activeplayer("#player"+k);
		$("#player"+k).animate({"opacity": "1"},50);
		if (k == 1){
			pl1move();
		}
		//PREFLOP
		else{
			$.post("Engine?analys="+k);
			if (move==0){
				$.get("Engine?preflopdecide",function(data){
					if (data == 0){
						fold(k-1);
					}
					if (data == 1){
						call(k-1);
					}
					if (data == 2){
						if (bet == bb*4){
							call(k-1);
						}
						else {
							raise(k-1);
						}
					}
				},"text");
			}
			//FLOP AND TURN
			else if (move==1 || move==2){
				$.get("Engine?flopdecide="+k+"",function(data){ 
					data = parseInt(data);
					if (data == 2){			
						if (bet == bb*4){
							call(k-1);
						}
						else {
							raise(k-1);
						}
					}
					else if (data == 1){
						call(k-1);
					}
					else if (data == 0){
						if (bet == 0){
							check(k-1);
						}
						else{
							fold(k-1);
						}
					}
				},"text");		
			}
			//RIVER
			else if (move==3){
				if (bet == bb*4){
					call(k);
				}
				else {
					raise(k);
				}
			}
			if(ingame[k-1]==1){
				notactiveplayer("#player"+k);
			}
			for (i=1;i<=ingame.length;i++){
				$("#pl"+i+"_state").html("");
			}
		}
	}
	//if the player has folded
	else{
		time=0;
	}
	//End game control
	endcheck();
}
function pl1move() {
	//Move of the player
	if (ingame.sum()==1){
		endgame();
	}
	else{
		if (plbet[0] == bet){
			$("#check").css("opacity", "1");
			$("#check").attr("onClick", "plcheck()");
			$("#raise").css("opacity", "1");
			$("#raise").attr("onClick", "plraise()");
		}
		if (bet==4*bb && plbet[0]<bet){
			$("#call").css("opacity", "1");
			$("#call").attr("onClick", "plcall()");
			$("#fold").css("opacity", "1");
			$("#fold").attr("onClick", "plfold()");
		}
		if (plbet[0] < bet && plbet[0] != bb*4){
			$("#fold").css("opacity", "1");
			$("#fold").attr("onClick", "plfold()");
			$("#call").css("opacity", "1");
			$("#call").attr("onClick", "plcall()");
			$("#raise").css("opacity", "1");
			$("#raise").attr("onClick", "plraise()");
		}
		if (plbet[0] == bb*4){
			//if the bet is maksimum, goes on
			hidebuttons();
			return
		}	
		wait();
	}
}
//GAME END/RESTART
function endgame(){
	//count bank
	bank+=plbet.sum();
	$(".playerbet").css("opacity", "0");
	$("#bank").html(bank);
	//show the cards
	mes="Engine?";
	for (var i=1;i<=ingame.length;i++){
		if (ingame[i-1]==1){
			mes+="plcards="+i;
			if (mes.length!=7){
				mes+="&";
			}
		}
	}
	$.get(mes,function(data){
		num=data.split(" ");
		for (i=0;i<num.length;i+=3){
			$("#player"+(num[i+2])+"_1").attr("src", cardlinks[num[i]]);
			$("#player"+(num[i+2])+"_2").attr("src", cardlinks[num[i+1]]);
		}
	},"text");
	var winner=0;
	if (ingame.sum()==1){
		for (var i=0;i<ingame.length;i++){
			if (ingame[i]==1){
				winner=i+1;
				activeplayer("#player"+winner);
				$("#player"+winner).css({"border": "3px yellow solid","opacity":"1"});
				$("#pl"+winner+"_state").html("WINNER");
				banks[winner-1]+=bank;  bank=0; k=winner;
			}
		}
	}
	else{
		$.get("Engine?findwinner",function(data){
			winner = parseInt(data);
			activeplayer("#player"+winner);
			$("#player"+winner).css({"border": "3px yellow solid","opacity":"1"});
			$("#pl"+winner+"_state").html("WINNER");
			banks[winner-1]+=bank;  bank=0; k=winner;
		},"text");
	}
	//analyse cards here and show the winner(k)
	//winner=k;
	//hightlight the winner, give the prize
	
	//bank = 0
	//return to startgame state
	$("#continue").css("opacity", "1");
	$("#continue").attr("onClick", "returntostartgamestate()");
	//setTimeout('returntostartgamestate()',10000);
}
function returntostartgamestate(){
	$(".players").css("opacity", "0.5");
	$("#bank").html(bank);
	$("#pl"+k+"_bank").html(banks[k-1]);
	hidebuttons();
	//return player cards, delete status, winner border, hide player bets
	for (var i=0;i<ingame.length;i++){
		$("div#pl_"+(i+1)+"_bet").css("opacity", "0");
		$("#pl"+(i+1)+"_state").html("");
		$("#player"+(i+1)).css({"border": "3px grey groove"});
		$("#player"+(i+1)+"_1").attr("src", "images/card_back.jpg");
		$("#player"+(i+1)+"_2").attr("src", "images/card_back.jpg");
		if ($("#player"+(i+1)).css("height")=="46px"){
			notactiveplayer("#player"+(i+1));
		}
		if (ingame[i]==1){
			returnacard("#player"+(i+1)+"_1");
			returnacard("#player"+(i+1)+"_2");
		}
	}
	//return table cards
	for (var i=1;i<6;i++){
		returnacard("#t_card"+i);
	}
	//change dealer
	$("#dealer"+dealer).css("opacity", "0");
	if (dealer==9){
		dealer=1;
	}
	else{
		dealer+=1;
	}
	//All variables but banks to their initial state
	plbet = [0, 0, 0, 0, 0, 0, 0, 0, 0];
	ingame = [1, 1, 1, 1, 1, 1, 1, 1, 1];
	move = 0;
	t = 9;
	counter = 0;
	sb = 2;
	bb = 4;
	bank = 0;
	bet = 4;
	//Start the next game
	setTimeout('startgame()',3000);
}
//COMPUTER ACTIONS
function check(k){
	banks[k-1] = banks[k-1] - bet + plbet[k-1];// player's bank
	plbet[k-1] = bet;//player's bet
	$("#pl"+k+"_bank").html(banks[k-1]);
	$("div#pl_"+k+"_bet").html(plbet[k-1]);
	$("div#pl_"+k+"_bet").css("opacity", "1");
}
function fold(k){
	$("#pl"+k+"_state").html("FOLD");
	returnacard("#player"+k+"_1");
	returnacard("#player"+k+"_2");
	ingame[k-1]=0;
}
function call(k){
	banks[k-1] = banks[k-1] - bet + plbet[k-1];// player's bank
	plbet[k-1] = bet;//player's bet
	$("#pl"+k+"_bank").html(banks[k-1]);
	$("div#pl_"+k+"_bet").html(plbet[k-1]);
	$("div#pl_"+k+"_bet").css("opacity", "1");
}
function raise(k){
	bet = bet + bb; // bet raises per 1 bb	
	banks[k-1] = banks[k-1] - bet + plbet[k-1];// player's bank
	plbet[k-1] = bet;	//player's bet
	$("#pl"+k+"_bank").html(banks[k-1]);
	$("div#pl_"+k+"_bet").html(plbet[k-1]);
	$("div#pl_"+k+"_bet").css("opacity", "1");
}
//PLAYER ACTIONS
function plfold(){
	//Drop cards
	$("#pl1_state").html("FOLD");
	returnacard("#player1_1");
	returnacard("#player1_2");
	hidebuttons();
	ingame[0]=0;
	endcheck();
}
function plraise(){

	//Raise players bet
	if (bet == bb*4){
		plcall();
	}
	else {
		bet = bet + bb; // bet raises per 1 bb	
		banks[0] = banks[0] - bet + plbet[0];// player's bank
		plbet[0] = bet;	//player's bet
		$("#pl1_bank").html(banks[0]);
		$("div#pl_1_bet").html(plbet[0]);
		$("div#pl_1_bet").css("opacity", "1");	
	}
	notactiveplayer("#player1");
	hidebuttons();
	endcheck();
}
function plcall(){
	//Same bet as the other players
	banks[0] = banks[0] - bet + plbet[0];// player's bank
	plbet[0] = bet;	//player's bet
	$("#pl1_bank").html(banks[0]);
	$("div#pl_1_bet").html(plbet[0]);
	$("div#pl_1_bet").css("opacity", "1");

	hidebuttons();
	notactiveplayer("#player1");
	endcheck();
}
function plcheck(){
	//Do nothing
	hidebuttons();
	notactiveplayer("#player1");
	endcheck();
}
//FUNCTIONS
function check(){

	//check if all active players have same bets, if round complete - next turn
	var a=0;
	var b=0;
	for (i=0; i < ingame.length; i++){
		if (ingame[i]==1 && plbet[i]==bet){
			a+=1;
		}
		if (ingame[i]==1){
			b+=1;
		}
	}
	if (a == b){
		return 1;	
	}
	else {
		return 0;
	}
}
function endcheck(){
	$("#player"+k).animate({"opacity": "0.5"},1000);
	if (ingame.sum()==1)setTimeout('endgame()',time);
	else if (check() == 1 && move==0 && counter>=ingamen)setTimeout('flop()',time);
	else if (check() == 1 && move==1 && counter>=ingamen)setTimeout('turn()',time);
	else if (check() == 1 && move==2 && counter>=ingamen)setTimeout('river()',time);
	else if (check() == 1 && move==3 && counter>=ingamen)setTimeout('endgame()',time);
	else{
		k+=1;
		setTimeout('plmove()',time);
	}
}
Array.prototype.sum = function(){
	for(var i=0,sum=0;i<this.length;sum+=this[i++]);
	return sum;
};
function hidebuttons(){
	//Hide all buttons from the player, so that he cannot affect the game while it is not his turn
	$("#continue").css("opacity", "0");
	$("#continue").attr("onClick", "");
	$("#bet").css("opacity", "0");
	$("#bet").attr("onClick", "");
	$("#fold").css("opacity", "0");
	$("#fold").attr("onClick", "");
	$("#call").css("opacity", "0");
	$("#call").attr("onClick", "");
	$("#raise").css("opacity", "0");
	$("#raise").attr("onClick", "");
	$("#check").css("opacity", "0");
	$("#check").attr("onClick", "");
}
function dealers(a) {
	//Sets a dealer and compulsory bets (small blind and big blind)
	$("img#dealer"+a).css("opacity", "1");//Shows, who is the dealer
	dealer=a;
	k=a+3;
	if (a==9){
		a=0;
	}
	banks[a] = banks[a]-sb;
	plbet[a] = plbet[a] + sb;
	$("div#pl_"+(a+1)+"_bet").html(plbet[a]);//
	$("#pl"+(a+1)+"_bank").html(banks[a]);
	$("div#pl_"+(a+1)+"_bet").css("opacity", "1");
	if (a==8){
		a=-1;
	}
	$("div#pl_"+(a+2)+"_bet").html(bb);
	$("#pl"+(a+2)+"_bank").html(banks[a+1]-bb);
	banks[a+1] = banks[a+1]-bb;
	plbet[a+1] = plbet[a+1] + bb;
	$("div#pl_"+(a+2)+"_bet").css("opacity", "1");
}
//ANALYSIS
